package com.zimi.controller;

import com.zimi.model.WfActivityLine;
import com.zimi.model.WfActivityLineVO;
import com.zimi.model.WfForm;
import com.zimi.model.WfFormItem;
import com.zimi.service.WfActivityLineService;
import com.zimi.service.WfWorkflowApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("流程管理")
@Controller
@RequestMapping("wf/activity/line")
public class WfActivityLineApi {

    @Autowired
    private WfActivityLineService service;
    @Autowired
    private WfWorkflowApplicationService wfWorkflowApplicationService;

    /*@PostMapping("list")
    @ResponseBody
    @ApiOperation("查询所有")
    public List<WfActivity> selectAll(WfActivity record) {
        return service.selectAll(record);
    }*/

    @PostMapping("list")
    @ResponseBody
    @ApiOperation("查询所有")
    public Object list(WfActivityLine record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", service.selectAll(record));
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }

    @GetMapping("{id}")
    @ResponseBody
    @ApiOperation("根据Id查询信息")
    public WfActivityLine selectByPrimaryKey(@PathVariable("id") int id) {
        return service.selectByPrimaryKey(id);
    }

    @DeleteMapping("{id}")
    @ResponseBody
    @ApiOperation("根据Id删除")
    public boolean deleteByPrimaryKey(@PathVariable("id") int id) {
        return service.deleteByPrimaryKey(id) > 0 ? true : false;
    }

    @PostMapping("record")
    @ResponseBody
    @ApiOperation("添加")
    public boolean insert(WfActivityLine record) {
        return service.insertSelective(record) > 0 ? true : false;
    }

    @PutMapping("record")
    @ResponseBody
    @ApiOperation("根据条件修改信息")
    public boolean updateByPrimaryKey(WfActivityLine record) {
        return service.updateByPrimaryKeySelective(record) > 0 ? true : false;
    }

    @PostMapping("createNewActivityLine")
    @ResponseBody
    @ApiOperation("创建活动连线")
    public Object createNewActivityLine(WfActivityLine wfActivityLine) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            List<WfActivityLine> nextwfActivityLineList = service.selectAll(wfActivityLine);
            if (nextwfActivityLineList != null && nextwfActivityLineList.size() > 0) {
                if (nextwfActivityLineList != null && nextwfActivityLineList.size() > 0) {
                    objectMap.put("OK", "error");
                    objectMap.put("code", "400");
                    objectMap.put("message", "下一活动已关联，不能重复配置！");
                    return objectMap;
                }
            }
            service.insertSelective(wfActivityLine);
        } catch (Exception e) {
            objectMap.put("OK", "error");
            objectMap.put("code", "400");
            objectMap.put("message", e.getMessage());
        }
        objectMap.put("OK", "OK");
        return objectMap;
    }

    @PostMapping("getNodeData")
    @ResponseBody
    @ApiOperation("创建活动连线")
    public Object getNodeData(WfActivityLine wfActivityLine) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("List", service.selectAll(wfActivityLine));
            WfForm wfForm = service.getWfForm(wfActivityLine);
            List<WfFormItem> formItemList = new ArrayList<WfFormItem>();
            if (wfForm != null && wfForm.getFormHtml() != null && wfForm.getFormHtml().length() > 0) {
                formItemList = wfWorkflowApplicationService.getFormItems(wfForm.getFormHtml());
            }
            map.put("formField", formItemList);
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("code", 400);
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    @PostMapping("saveActivityLine")
    @ResponseBody
    @ApiOperation("保存活动连线数据")
    public Object saveActivityLine(WfActivityLine wfActivityLine) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            service.saveActivityLine(wfActivityLine);
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("code", 400);
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    @PostMapping("delLineBySourceTargetWfid")
    @ResponseBody
    @ApiOperation("删除活动连线（条件为流程id+sourceId+targetId）")
    public Object delLineBySourceTargetWfid(WfActivityLineVO wfActivityLineVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            service.delLineBySourceTargetWfid(wfActivityLineVO);
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("code", 400);
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }
}
